import 'dart:async';

import 'package:flutter/material.dart';
import 'package:shopping_group/event/event.dart';
import 'package:shopping_group/modle/more/entity/group_detail_entity.dart';
import 'package:shopping_group/modle/play/bloc/pingtuan_play_ok_bloc.dart';
import 'package:shopping_group/res/colors.dart';
import 'package:shopping_group/res/dimens.dart';
import 'package:shopping_group/utils/object_util.dart';
import 'package:shopping_group/utils/route_util.dart';
import 'package:shopping_group/utils/widget_utils.dart';
import 'package:shopping_group/view/button_progress_view.dart';

import 'yaoqing_page.dart';

class PingtuanPlayokPage extends StatefulWidget {
  final GroupDetailEntity entity;
  PingtuanPlayokPage({Key key, this.entity}) : super(key: key);

  @override
  _PingtuanPlayokPageState createState() => _PingtuanPlayokPageState();
}

class _PingtuanPlayokPageState extends State<PingtuanPlayokPage> {
  StreamSubscription _isZhongjiangSubscription;
  String _beZonJian = '';
  PingtuanPlayOkBloc _bloc;
  @override
  void initState() {
    super.initState();
    _bloc = PingtuanPlayOkBloc();
    if (!ObjectUtil.isEmptyString(this.widget.entity.joinId)) {
      _bloc.initDate.add(this.widget.entity.joinId);
    }

    _isZhongjiangSubscription = eventBus.on<IsZhongJiangEvent>().listen((event) {
      if (!mounted) {
        return;
      }
      setState(() {
        _beZonJian = event.beZonJian;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: WidgetUtils.showAppar(context, '推广成功', onTapClose: () {
        RouteUtil.closeView();
      }),
      body: Container(
          height: double.infinity,
          decoration: new BoxDecoration(
            color: Colours.title_white,
          ),
          child: Column(
            children: <Widget>[
              _shoppingView(),
              WidgetUtils.drawCodeDivider(color: Colours.themeColor),
              SizedBox(
                height: 16,
              ),
              Padding(
                padding: EdgeInsets.symmetric(horizontal: 16),
                child: WidgetUtils.getTextColorView(
                    'Y' == _beZonJian ? '恭喜！本次代理券提交成功！\n注：成功提交代理券累计达到规定数量后将返回！' : '本次提交代理券已返回！\n广告红利已发放！',
                    Colours.themeColor,
                    Dimens.FONT_SIZE_18),
              ),

              SizedBox(
                height: 16,
              ),
              // WidgetUtils.getTextColorView('Y' == _beZonJian ? '已中奖' : '未中奖',
              //     Colours.blackground_greed_70, Dimens.FONT_SIZE_16),
              // SizedBox(
              //   height: 16,
              // ),
              // WidgetUtils.getTextColorView(
              //     '请留意开奖通知', Colours.gray_33, Dimens.FONT_SIZE_14),
              SizedBox(
                height: 32,
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: <Widget>[
                  Expanded(
                      child: ButtonProgressView.crateCupertinoView('邀请好友推广',
                          blackColor: Colours.themeColor, textColor: Colours.title_white, cupertinoCall: () {
                    RouteUtil.goParameter(YaoQingPage());
                  })),
                  Expanded(
                    child: ButtonProgressView.crateCupertinoView(' 返回首页 ', cupertinoCall: () {
                      Navigator.of(context).popUntil((route) => route.isFirst);
                    }),
                  ),
                ],
              ),
            ],
          )),
    );
  }

  Widget _shoppingView() {
    return Container(
      decoration: BoxDecoration(
        color: Colours.title_white,
      ),
      padding: const EdgeInsets.symmetric(vertical: 16, horizontal: 8),
      child: Row(
        children: <Widget>[
          Expanded(
            child: Container(
              height: 110.0,
              width: 110.0,
              child: FadeInImage.assetNetwork(
                image: this.widget.entity.imgUrl,
                placeholder: 'images/picture_loading.png' /* 指定gif */,
                fit: BoxFit.contain,
              ),
            ),
            flex: 2,
          ),
          Expanded(
            child: Padding(
              padding: const EdgeInsets.only(left: 16),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  WidgetUtils.getTextColorView(
                    '${_getType(this.widget.entity)}元${this.widget.entity.sucMenCt}人推广',
                    Colours.text_2222,
                    Dimens.FONT_SIZE_14,
                  ),
                  SizedBox(
                    height: Dimens.FONT_SIZE_16,
                  ),
                  WidgetUtils.getTextColorView(
                    '注意：\n提交相关金额代理卷\n获得相应资格推广权',
                    Colours.gray_91,
                    Dimens.FONT_SIZE_14,
                  ),
                  SizedBox(
                    height: 8,
                  ),
                  // WidgetUtils.getTextColorView(
                  //   //'¥99/件 共1件商品',
                  //   '¥${this.widget.entity.rechargeVal}/件 共1件商品',
                  //   Colours.themeColor,
                  //   Dimens.FONT_SIZE_14,
                  // ),
                ],
              ),
            ),
            flex: 3,
          ),
        ],
      ),
    );
  }

  String _getType(GroupDetailEntity entity) {
    String type = '99';
    if (ObjectUtil.isEmptyString(entity.groupSpecType)) {
      return type;
    }

    List<String> list = entity.groupSpecType.split('_');
    if (ObjectUtil.isEmptyList(list) || 1 == list.length) {
      return type;
    }

    return list[1];
  }

  @override
  void dispose() {
    _isZhongjiangSubscription?.cancel();
    super.dispose();
  }
}
